package pATT.bNEdit.base;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.sun.org.apache.xerces.internal.parsers.SAXParser;

public class XmlDataParser extends org.xml.sax.helpers.DefaultHandler implements Serializable{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;


public XmlDataParser() {
  }

  HashMap nodes;
  List dataNode;
  List nodesNames;
  StringBuffer accumulator; // Accumulate text
  String nodeName; // Remember text


  // Called at the beginning of parsing.  We use it as an init() method
  public void startDocument() {
    accumulator = new StringBuffer();
    nodes = new HashMap();
    dataNode  = new ArrayList();
    nodesNames = new ArrayList();
  }

  // When the parser encounters plain text (not XML elements), it calls
  // this method, which accumulates them in a string buffer.
  // Note that this method may be called multiple times, even with no
  // intervening elements.
  public void characters(char[] buffer, int start, int length) {
    accumulator.append(buffer, start, length);
  }

  // At the beginning of each new element, erase any accumulated text.
  public void startElement(String namespaceURL, String localName,
                           String qname, Attributes attributes) {
    accumulator.setLength(0);
    if (localName.equals("NODE"))
      dataNode = new ArrayList();
  }

  // Take special action when we reach the end of selected elements.
  // Although we don't use a validating parser, this method does assume
  // that the XML file we're parsing is valid.
  @SuppressWarnings("unchecked")
public void endElement(String namespaceURL, String localName, String qname) {
    if (localName.equals("NAME")) { // Store servlet name
      nodeName = accumulator.toString().trim();
      nodesNames.add(nodeName);
    }
    else if (localName.equals("DATA")) { // Store servlet class
      dataNode.add(accumulator.toString().trim());
    }
    else if (localName.equals("NODE")) { // Store servlet pattern
      nodes.put(nodeName, dataNode);
    }
  }

  // Called at the end of parsing.  Used here to print our results.
  public void endDocument() {

  }

  // Issue a warning
  public void warning(SAXParseException exception) {
    System.err.println("WARNING: line " + exception.getLineNumber() + ": " +
                       exception.getMessage());
  }

  // Report a parsing error
  public void error(SAXParseException exception) {
    System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
                       exception.getMessage());
  }

  // Report a non-recoverable error and exit
  public void fatalError(SAXParseException exception) throws SAXException {
    System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
                       exception.getMessage());
    throw (exception);
  }

  public HashMap getNodes(){
    return nodes;
  }

  public List getDataOfNode(String nameNode){
    return (ArrayList)nodes.get(nameNode);
  }

  public List getNodesNames(){
    return (ArrayList)nodesNames;
  }


  public static void main(String[] args) throws IOException, SAXException {
  // Create the parser we'll use.  The parser implementation is a
  // Xerces class, but we use it only through the SAX XMLReader API
  org.xml.sax.XMLReader parser = new SAXParser();

  // Specify that we don't want validation.  This is the SAX2
  // API for requesting parser features.  Note the use of a
  // globally unique URL as the feature name.  Non-validation is
  // actually the default, so this line isn't really necessary.
  parser.setFeature("http://xml.org/sax/features/validation", false);

  // Instantiate this class to provide handlers for the parser and
  // tell the parser about the handlers
  XmlDataParser handler = new XmlDataParser();
  parser.setContentHandler(handler);
  parser.setErrorHandler(handler);

  // Create an input source that describes the file to parse.
  // Then tell the parser to parse input from that source
  org.xml.sax.InputSource input = new InputSource("C:\\DatosUsuario-106.xml");
  parser.parse(input);

}



}